﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;

namespace NJL.Web
{
    public partial class News : BasePageVisitor
    {
        private readonly BLL.NJL_SYS_NEWS bll = new BLL.NJL_SYS_NEWS();
        private readonly BLL.NJL_USER bllUser = new BLL.NJL_USER();
        private readonly BLL.NJL_SYS_NEWS_COMMENT bllSysNewsComment = new BLL.NJL_SYS_NEWS_COMMENT();
        private readonly BLL.NJL_SYS_CS_NEWS_TYPE bllSysNewsType = new BLL.NJL_SYS_CS_NEWS_TYPE();
        private readonly BLL.NJL_PERSON bllPerson = new BLL.NJL_PERSON();
        private readonly BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();
        private readonly BLL.NJL_SYS_NEWS_CHAPTER newsChapter = new BLL.NJL_SYS_NEWS_CHAPTER();

        protected Model.NJL_SYS_NEWS MODEL;
        protected Model.NJL_SYS_CS_NEWS_TYPE MODEL_TYPE;
        protected Model.NJL_SYS_NEWS_COMMENT modelSysNewsComment = new Model.NJL_SYS_NEWS_COMMENT();
        protected Model.NJL_USER modelUser = new Model.NJL_USER();
        protected Model.NJL_PERSON modelPerson;
        protected Model.NJL_COMPANY modelCompany;

        protected IList<Model.NJL_SYS_NEWS> LIST_SYS_NEWS;

        DataSet ds = new DataSet();
        DataTable dt = new DataTable("news");
        DataRow row;
        DataColumn column;

        protected void Page_Load(object sender, EventArgs e)
        {
            LIST_SYS_NEWS = bll.GetModelList(8, " NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' AND TYPE_CODE='17' AND IS_PUBLISH=1 ", "PUBLISH_DATE_TIME DESC");
            MODEL = bll.GetModel(Request.QueryString["id"]);
            MODEL_TYPE = bllSysNewsType.GetModel(MODEL.TYPE_CODE);
            if (!IsPostBack)
            {
                LoadData();
                LoadPageSEO();
            }
   
        }
        /// <summary>
        /// 加载页面SEO
        /// </summary>
        private void LoadPageSEO()
        {
            Page.Title = MODEL.SEO_TITLE;
            HtmlMeta myMeta = new HtmlMeta();
            myMeta.Name = "Keywords";
            myMeta.Content = MODEL.SEO_KEYWORDS;
            this.Header.Controls.Add(myMeta);
            HtmlMeta myMeta1 = new HtmlMeta();
            myMeta1.Name = "Description";
            myMeta1.Content = MODEL.SEO_DESCRIPTION;
            this.Header.Controls.Add(myMeta1);
        }


        private void LoadData()
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != "")
            {
                if (MODEL == null)
                {
                    Response.Redirect("/Default.aspx", true);
                }
                //获取新闻内容和新闻类别内容
                MODEL = bll.GetModel(Request.QueryString["id"]);


                DataSet dscomment = bllSysNewsComment.GetList("NJL_SYS_NEWS_ID='" + Request.QueryString["id"].ToString() + "'");
                rptCommentList.DataSource = dscomment;
                rptCommentList.DataBind();
                litCommentCount.Text = dscomment.Tables[0].Rows.Count.ToString();

                ds.Tables.Add(dt);
                column = new DataColumn();
                column.DataType = System.Type.GetType("System.String");
                column.Caption = "newsContent";
                column.ColumnName = "newsContent";
                dt.Columns.Add(column);
                row = dt.NewRow();
                row["newsContent"] = MODEL.NEWS_CONTECT;
                dt.Rows.Add(row);
                DataSet ds1 = newsChapter.GetList("NJL_SYS_NEWS_ID='" + Request.QueryString["id"] + "' order by CHAPTER_CODE asc");
                if (ds1.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow rows in ds1.Tables[0].Rows)
                    {
                        row = dt.NewRow();
                        row["newsContent"] = rows["CHAPTER_CONTENT"];
                        dt.Rows.Add(row);
                    }
                }
                string[] param = { "id" };
                if (ds1.Tables[0].Rows.Count > 0)
                {
                    pager.InnerHtml = Royalsoft.Common.Pager.GetPageNum(ds, rptContent, 1, param, "/xckb/" + MODEL.TYPE_CODE+ "/");
                }
                else
                {
                    Royalsoft.Common.Pager.GetPageNum(ds, rptContent, 1, param);
                }
   
            }
            else
            {
                Response.Redirect("/Default.aspx", true);
            }
        }

 

        protected void btnComment_Click(object sender, EventArgs e)
        {
            if (UserId == "" || UserType == "-1")
            {
                Royalsoft.Common.MessageBox.ShowAndRedirect(this, "请登录后再发表", "/Login.aspx?url=" + Request.RawUrl.ToString());
                return;
            }
            else
            {
                if (Session["CheckCode"] != null)
                {
                    if (txtValidate.Text.Trim().ToLower() != Session["CheckCode"].ToString().ToLower())
                    {
                        Royalsoft.Common.MessageBox.Show(this, "请输入正确的验证码");
                        return;
                    }
                    modelSysNewsComment.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                    modelSysNewsComment.NEWS_COMMENT = txtComment.Text.Trim();
                    modelSysNewsComment.CREATE_USER_ID = new BasePage().UserId;
                    modelSysNewsComment.CREATE_USER_NAME = new BasePage().UserName;
                    modelSysNewsComment.CREATE_DATE_TIME = DateTime.Now;
                    modelSysNewsComment.NJL_SYS_NEWS_ID = Request.QueryString["id"].ToString();
                    if (bllSysNewsComment.Add(modelSysNewsComment))
                    {
                        string message;
                        if (HandleIntegral(UserId, "3", out message))
                        {

                        }
                        Royalsoft.Common.MessageBox.ShowAndRedirect(this, "评论成功+2分",Request.RawUrl);
                    }
                }
                else
                {
                    Royalsoft.Common.MessageBox.ShowAndRedirect(this, "连接系统超时", "" + Request.RawUrl.ToString());
                }

            }
        }
        protected void rptCommentList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView grv = (DataRowView)e.Item.DataItem;
                string userId = grv["CREATE_USER_ID"].ToString();
                Image imgUserHead = e.Item.FindControl("imgUserHead") as Image;
                modelPerson = bllPerson.GetModelByUserId(userId);
                if (modelPerson != null)
                {
                    if (modelPerson.IMAGE_CODE != "")
                    {
                        imgUserHead.ImageUrl = GetImageUrl(modelPerson.IMAGE_CODE, "U2");
                        imgUserHead.BorderWidth = 1;
                    }
                    else
                    {
                        imgUserHead.ImageUrl = "/Images/avatar.jpg";
                        imgUserHead.BorderWidth = 1;
                    }
                }
            }
        }
    }
}